package com.init.engineering.service.impl;

import com.init.engineering.dao.BranchDao;
import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Branch;
import com.init.engineering.service.BranchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BranchServiceImpl implements BranchService {
    @Autowired(required = false)
    private BranchDao branchDao;


    @Override
    public List<Branch> findBranches() {
        List<Branch> branches = branchDao.findBranches();
        return branches;
    }

    @Override
    public PageResult findBranchesByPage(QueryPageBean queryPageBean) {
        Integer begin = (queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize();
        queryPageBean.setCurrentPage(begin);
        //查询当前页的数据集合
        List<Branch> branches = branchDao.findBranchByPage(queryPageBean);
        //获取分页查询的条件
        String queryString = queryPageBean.getQueryString();
        //获取分页查询按条件查询的总条数
        long totalCount = branchDao.selectTotalCount(queryString);

        //将数据封装到PageResult内
        PageResult pageResult = new PageResult();
        pageResult.setTotal(totalCount);
        pageResult.setRows(branches);
        return pageResult;
    }

    @Override
    public int saveBranch(Branch branch) {
        int rows = branchDao.saveBranch(branch);
        return rows;
    }

    @Override
    public int deleteBranch(String bid) {
        int rows = branchDao.updateBranchStatus(bid);
        return rows;
    }

    @Override
    public Branch findBranchByBid(String bid) {
        Branch branch = branchDao.findBranchByBid(bid);
        return branch;
    }

    @Override
    public int updateBranch(Branch branch) {
        int rows = branchDao.updateBranch(branch);
        return rows;
    }

    @Override
    public List<Branch> findBranchesByPid(int pid) {
        List<Branch> branches = branchDao.findBranchByPid(pid);
        return branches;
    }
}
